package com.vnp.cooking;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.viewpagerindicator.LinePageIndicator;
import com.vnp.cooking.adapter.ViewPagerStepCookpadAdapter;
import com.vnp.cooking.bean.ObjCachlam;
import com.vnp.cooking.bean.ObjCookpad;
import com.vnp.cooking.httpclient.HttpClientHelper;
import com.vnp.cooking.httpclient.MyJsonHttpResponseHandler;
import com.vnp.cooking.utils.Utils;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.renderscript.Type;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

public class StepbyCookpadActivity extends FragmentActivity implements
		OnClickListener {
	// Khai bao cac thanh phan
	ViewPager viewPager;
	ViewPagerStepCookpadAdapter adapter;
	String[] txtStep = {};
	String[] txtDecription = {};
	String[] imgStep = {};
	LinePageIndicator indicator;
	ImageButton btnBack;
	TextView titleMonan;
	TextView txtNoResult;

	ArrayList<ObjCachlam> objCachlams = new ArrayList<ObjCachlam>();
	ObjCookpad objCookpad;
	ProgressBar progressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_stepby_cookpad);
		objCookpad = getIntent().getParcelableExtra("objCookpad");
		getBuoclam();
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		titleMonan = (TextView) findViewById(R.id.txt_monan_title);
		txtNoResult = (TextView) findViewById(R.id.txt_no_buoc_lam);
		// locate view pager in activity_stepby_cookpad
		viewPager = (ViewPager) findViewById(R.id.pager);
		indicator = (LinePageIndicator) findViewById(R.id.indicator);
		progressBar = (ProgressBar) findViewById(R.id.progressBarBuoclam);
		// get results to adapter
		adapter = new ViewPagerStepCookpadAdapter(StepbyCookpadActivity.this,
				objCachlams);
		// Bind adater to view pager
		viewPager.setAdapter(adapter);
		viewPager.setPageMargin(20);
		indicator.setViewPager(viewPager);

		setFont();
		
		init();
	}
	
	void setFont(){
		titleMonan.setTypeface(Utils.getTypefaceRegular(StepbyCookpadActivity.this), Typeface.BOLD);
	}

	void init() {
		if (objCookpad.getNameCookpad().length() > 20) {
			titleMonan.setText(objCookpad.getNameCookpad().substring(0, 20)
					+ "...");
		} else {
			titleMonan.setText(objCookpad.getNameCookpad());
		}
		// set Onclick cho nut back
		btnBack.setOnClickListener(this);
		txtNoResult.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			closeActivity();
			break;
		case R.id.txt_no_buoc_lam:
			getBuoclam();
			break;

		default:
			break;
		}

	}

	public void getBuoclam() {
		HttpClientHelper httpClientHelper = new HttpClientHelper(
				new MyJsonHttpResponseHandler() {

					@Override
					public void onFailure(Throwable error) {
						super.onFailure(error);
						hiddenProgessDialog();
						txtNoResult.setVisibility(View.VISIBLE);

					}

					@Override
					public void onSuccess(JSONObject re) {
						hiddenProgessDialog();
						txtNoResult.setVisibility(View.GONE);
						try {
							int status = re.getInt("status");
							if (status == 200) {
								JSONArray data = re.getJSONArray("data");
								Log.i("DATA", "BUOC LAM: " + data);
								for (int i = 0; i < data.length(); i++) {
									JSONObject step = data.getJSONObject(i);
									String desc = step.getString("desc");
									String urlImage = step.getString("image");
									/*
									 * txtDecription[i] = desc; imgStep[i] =
									 * urlImage; txtStep[i] = String.valueOf(i);
									 */
									ObjCachlam objCachlam = new ObjCachlam();
									objCachlam.setDecriptionStep(desc);
									objCachlam.setUrlImageStep(urlImage);
									objCachlam.setStep(i + 1);
									objCachlams.add(objCachlam);
									viewPager.getAdapter()
											.notifyDataSetChanged();
									Log.i("DATA", "Buoc " + (i + 1) + " "
											+ objCachlam.getDecriptionStep());
								}

							} else {
								hiddenProgessDialog();
								txtNoResult.setVisibility(View.VISIBLE);

							}

						} catch (JSONException e) {
							e.printStackTrace();
							hiddenProgessDialog();
							txtNoResult.setVisibility(View.VISIBLE);
						}

					}

				});

		httpClientHelper.getStepCongthuc(objCookpad.getId());
	}

	// hide progress bar
	void hiddenProgessDialog() {
		try {
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					try {
						progressBar.setVisibility(View.GONE);
					} catch (Exception e) {
					}
				}
			});
		} catch (Exception e) {
		}
	}

	void closeActivity() {
		finish();
		overridePendingTransition(R.anim.open_main, R.anim.close_next);
	}

	@Override
	public void onBackPressed() {
		closeActivity();
	}

}
